package com.cbg.es.controller;

import com.alibaba.fastjson.JSONObject;
import com.cbg.es.dto.EsColListDTO;
import com.cbg.es.dto.EsTableDTO;
import com.cbg.es.entity.EsTable;
import com.cbg.es.service.IEsColService;
import com.cbg.es.service.IEsTableService;
import com.cbg.es.vo.EsColEditVO;
import com.cbg.es.vo.EsColVO;
import com.cbg.vo.DelVO;
import com.cbg.vo.DirectionsVO;
import com.cbg.vo.StatusVO;
import com.shuyuwl.core.enums.StatusEnum;
import com.shuyuwl.core.support.ServerResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 表管理Controller
 *
 * @author wangjuzheng
 * @date 2019-10-24
 **/
@RestController
@Api(tags = "全景通查-字段管理")
@RequestMapping("/api/es/col")
@CrossOrigin(allowCredentials = "true", maxAge = 3600, allowedHeaders = "*", origins = "*")
public class EsColController {

    @Resource
    private IEsColService esColService;

    @Resource
    private IEsTableService esTableService;

    /**
     * 根据基础表id获取列信息
     *
     * @return 模型
     */
    @GetMapping(value = "/table/list")
    @ApiOperation(value = "表信息")
    public ServerResponse<List<EsTableDTO>> getEsColTables() {

        //获取列信息
        List<EsTable> esTables = esTableService.findAllEsTable(StatusEnum.OK.getCode());
        return ServerResponse.createBySuccess(JSONObject.parseArray(JSONObject.toJSONString(esTables), EsTableDTO.class));
    }

    /**
     * 根据基础表id获取列信息
     *
     * @param id 表id
     * @return 模型
     */
    @GetMapping(value = "/list/{id}")
    @ApiOperation(value = "根据基础表id获取列信息")
    public ServerResponse<List<EsColListDTO>> getEsCols(@ApiParam(value = "基础表id") @PathVariable Long id) {

        //获取列信息
        List<EsColListDTO> listDTOS = esColService.getEsCols(id);
        return ServerResponse.createBySuccess(listDTOS);
    }

    /**
     * 列的添加
     *
     * @param esColVO 列的添加模型
     * @return 模型
     */
    @PostMapping
    @ApiOperation(value = "列的添加")
    public ServerResponse addEsCol(@Validated @RequestBody EsColVO esColVO) {

        //列的添加
        esColService.addEsCOl(esColVO);
        return ServerResponse.createBySuccess("添加列成功");
    }

    /**
     * 列的修改
     *
     * @param esColEditVO 列的修改模型
     * @return 模型
     */
    @PutMapping
    @ApiOperation(value = "列的修改")
    public ServerResponse updateEsCol(@Validated @RequestBody EsColEditVO esColEditVO) {

        //列的修改
        esColService.updateEsCol(esColEditVO);
        return ServerResponse.createBySuccess("操作成功");
    }

    /**
     * 列状态的修改
     *
     * @param statusVO 列修改状态模型
     * @return 模型
     */
    @PutMapping("/list/status")
    @ApiOperation(value = "列状态的修改")
    public ServerResponse updateEsColStatus(@Validated @RequestBody StatusVO statusVO) {

        //列状态的修改
        esColService.updateEsColStatus(statusVO);
        return ServerResponse.createBySuccess("成功");
    }

    /**
     * 列的删除
     *
     * @param delVO 删除参数模型
     * @return 模型
     */
    @DeleteMapping("/list")
    @ApiOperation(value = "列的删除")
    public ServerResponse deleteEsCol(@Validated @RequestBody DelVO delVO) {

        //列的删除
        esColService.deleteEsCol(delVO.getIds());
        return ServerResponse.createBySuccess("删除成功");
    }

    /**
     * 获取列详细信息
     *
     * @param id 列id
     * @return 模型
     */
    @GetMapping(value = "/detail/{id}")
    @ApiOperation(value = "获取列详细信息")
    public ServerResponse<EsColListDTO> getDetails(@ApiParam(value = "列id") @PathVariable Long id) {

        //获取列详细信息
        EsColListDTO listDTOS = esColService.getDetails(id);
        return ServerResponse.createBySuccess(listDTOS);
    }

    /**
     * 列位置的移动
     *
     * @param directionsVO 移动VO
     * @return 模型
     */
    @PutMapping(value = "/directions")
    @ApiOperation(value = "列位置的移动")
    public ServerResponse updateDirections(@Validated @RequestBody DirectionsVO directionsVO) {

        //列位置的移动
        esColService.updateDirections(directionsVO);
        return ServerResponse.createBySuccess();
    }
}
